import { Ref } from "vue";

export const useHandleTree = (
  treeOptions: Ref<Array<any>>,
  treeRef: Ref<any>,
  form: Record<string, any>
) => {
  /** 树权限（展开/折叠）*/
  const handleCheckedTreeExpand = (value: any) => {
    console.log(value, treeRef, treeOptions);
    let treeList = treeOptions.value;
    for (let i = 0; i < treeList.length; i++) {
      const node = treeRef.value.store.nodesMap[treeList[i].id];
      if (node && node.expanded) node.expanded = value;
    }
  };
  /** 树权限（全选/全不选） */
  const handleCheckedTreeNodeAll = (value: any) => {
    //   console.log(value, type);
    treeRef.value.setCheckedNodes(value ? treeOptions.value : []);
  };
  /** 树权限（父子联动） */
  const handleCheckedTreeConnect = (value: any) => {
    //   console.log(value, type);
    // form.value.menuCheckStrictly = value ? true : false;
    form.menuCheckStrictly = value ? true : false;
  };

  /** 选择角色权限范围触发 */
  const dataScopeSelectChange = (value: any) => {
    if (value !== "2") {
      treeRef.value.setCheckedKeys([]);
    }
  };
  return {
    handleCheckedTreeExpand,
    handleCheckedTreeNodeAll,
    handleCheckedTreeConnect,
    dataScopeSelectChange,
  };
};

/** 更多操作 */
// const handleCommand = (command: string, row: any) => {
//     switch (command) {
//         case "handleDataScope":
//             handleDataScope(row);
//             break;
//         case "handleAuthUser":
//             handleAuthUser(row);
//             break;
//         default:
//             break;
//     }
// }
/** 根据角色ID查询菜单树结构 */
// const getRoleMenuTreeselect = (roleCode: string) => {
//     return roleMenuTreeselect(roleCode).then(response => {
//         menuOptions.value = response.menus;
//         return response;
//     });
// }

/** 所有菜单节点数据 */
// const getMenuAllCheckedKeys = () => {
//     // 目前被选中的菜单节点
//     let checkedKeys = menuRef.value.getCheckedKeys();
//     // 半选中的菜单节点
//     let halfCheckedKeys = menuRef.value.getHalfCheckedKeys();
//     checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
//     return checkedKeys;
// }

// /** 所有部门节点数据 */
// function getDeptAllCheckedKeys() {
//     // 目前被选中的部门节点
//     let checkedKeys = deptRef.value.getCheckedKeys();
//     // 半选中的部门节点
//     let halfCheckedKeys = deptRef.value.getHalfCheckedKeys();
//     checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
//     return checkedKeys;
// }
